import VideoPreview from "../module/preview/VideoPreview";
import PdfPreview from "../module/preview/PdfPreview";
import DocPreview from "../module/preview/DocPreview";
import ImgPreview from "../module/preview/ImgPreview";
import CodePreview from "../module/preview/CodePreview";
import UnPreview from "../module/preview/UnPreview";
import {coreDispatch} from "../utils/StoreUtil";
import PreviewModal from "../component/modal/PreviewModal";
import TxtPreview from "../module/preview/TxtPreview";

const previewMap: any = {
    doc: {
        suffix: ['djvu', 'doc', 'docm', 'docx', 'docxf', 'dot', 'dotm', 'dotx', 'epub', 'fb2', 'fodt', 'htm', 'html', 'mht', 'mhtml', 'odt', 'oform', 'ott', 'oxps', 'rtf', 'stw', 'sxw', 'txt', 'wps', 'wpt', 'xml', 'xps', 'csv', 'et', 'ett', 'fods', 'ods', 'ots', 'sxc', 'xls', 'xlsb', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx', 'xml', 'dps', 'dpt', 'fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx', 'sxi', 'pdf'],
        component: DocPreview,
        props: {
            originUrl: "/office-html-view/office_preview.html"
        }
    },
    pdf: {
        suffix: ['pdf'],
        component: PdfPreview,
        props: {
            originUrl: "/pdfJs/web/viewer.html"
        }
    },
    img: {
        suffix: ["jpg", "jpeg", "png", "gif", "bmp"],
        component: ImgPreview,
        props: {}
    },
    video: {
        suffix: ["mp4", "flv"],
        component: VideoPreview,
        props: {
            originUrl: "/videoJs/index.html"
        }
    },
    code: {
        suffix: ["xml", "json"],
        component: CodePreview,
        props: {}
    },
    txt: {
        suffix: ["txt"],
        component: TxtPreview,
        props: {}
    },
    default: {
        suffix: [],
        component: UnPreview,
        props: {}
    }
}

const suffixMap: any = {};
const suffixList: string[] = []


// @ts-ignore
const createSuffixMap: Function = function (params: any = {}) {
    for (const key in previewMap) {
        previewMap[key].suffix.forEach((suffix: string) => {
            suffixList.push(suffix);
            suffixMap[suffix] = key;
        });
    }
    for (const key in params) {
        suffixMap[key] = params[key];
    }
}

// @ts-ignore
const getComConfigBySuffix: Function = function (suffix: string) {
    suffix = suffix?.toLocaleLowerCase();
    const key: string = suffixMap[suffix] || "default";
    return previewMap[key];
}

// @ts-ignore
const previewFile: Function = function (data: any, params: any = {}) {
    const {
        fileField = "file",
        suffixField = "extName",
        fileIdField = "id",
    }: {
        fileField: string,
        suffixField: string,
        fileIdField: string,
    } = params;

    coreDispatch("modalReducer/createModal", {
        key: "preview-" + data.id,
        modalProps: {
            open: true,
            width: "90vw",
            height: "85vh",
            destroyOnClose: "true",
            footer: null,
            wrapClassName: "sf_core_preview_modal"
        },
        component: PreviewModal,
        comProps: {
            ...params,
            data: data,
            fileField: fileField,
            fileIdField: fileIdField,
            suffixField: suffixField
        }
    });
};

const isDatasetFn: Function = function (suffix) {
    return suffixList.indexOf(suffix) !== -1;
}

export {
    createSuffixMap,
    getComConfigBySuffix,
    previewFile,
    isDatasetFn
}